<?php

namespace App\Admin\Controllers;

use App\Guardian;
use App\Http\Controllers\Controller;
use App\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Str;
use Overtrue\Pinyin\Pinyin;

class GuardianController extends Controller
{
    use ModelForm;
    private $header = '家长管理';
    private $states = [
        'on' => ['value' => 1, 'text' => '男', 'color' => 'primary'],
        'off' => ['value' => 2, 'text' => '女', 'color' => 'default'],
    ];

    public function __construct()
    {
        User::creating(function (User $user) {
            if (!$user->id) {
                $user->id = Str::uuid();
            }
            if (!$user->wasChanged('valid')) {
                $user->valid = 0;
            }
        });
        Guardian::saving(function ($guardian) {
            $pinyin = new Pinyin();
            $guardian->pinyin = implode('', $pinyin->convert($guardian->name));
            $guardian->first_letter = $pinyin->abbr($guardian->name);
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('新增');

            $content->body($this->form());
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Guardian::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('name', '名称')->editable();
            $grid->column('gender', '性别')->switch($this->states);
            $grid->column('detail', '描述')->limit(20)->editable('textarea');
            $grid->created_at('创建时间');

            $grid->filter(function ($filter) {

                // Sets the range query for the created_at field
                $filter->like('name', '学校名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Guardian::class, function (Form $form) {
            $form->id('id', 'ID');
            $form->text('name', '姓名');
            $form->switch('gender', '性别')->states($this->states)->default(0);
            $form->hidden('user_id');
            $form->hidden('user.id')->default((string)Str::uuid());
            $form->text('user.code', '编号');
            $form->text('user.name', '账号');
            $form->email('user.email', ' 邮箱');
            $form->password('user.password', '密码');
            $form->mobile('user.mobile', '手机号');
            $form->textarea('description', '描述');
            $form->saving(function (Form $form) {
                if (!$form->model()->exists) {
                    $form->input('user_id', $form->input('user.id'));
//                    $user_id = (string)Str::uuid();
//                    $form->input('user_id', $user_id);
//                    $form->input('user.id', $user_id);
////                    $form->model()->user()->create($form->input('user'));
                }
            });
//            $form->saving(function (Form $form) {
//                if (!$form->model()->exists) {
//                    $user = $form->input('user');
//                    $user['name'] = $form->input('name');
//                    $form->model()->user()->create($user);
//                    $form->hasMany()
////                    if (!$form->input('user_id')) {
////                        $user = User::create($user);
////                        $form->input('user_id', $user->id);
////                    } else {
////                        $user = $form->input('user');
////                        $user['name'] = $form->input('name');
////                        if (!$user['password']) {
////                            unset($user['password']);
////                        }
////                        User::find($form->input('user_id'))->update($user);
////                    }
//                } else {
////                    if(!$form->model()->user) {
////                        $user = User::create($form->input('user'));
////                        $user->name = $form->input('name');
////                        $form->model()->user_id = $user->id;
////                    }
//                }
//            });
        });
    }
}
